﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ESPL.CodeGenerator.Common;
using ESPL.CodeGenerator.Common.ProjectBuilders;
using ESPL.CodeGenerator.Common.Structs;
using ESPL.CodeGenerator.Domain.ProjectBuilders;
using ESPL.CodeGenerator.Common.Enums;

namespace ESPL.CodeGenerator.Domain
{
    public class CodeGeneratorFactory : ICodeGeneratorFactory
    {
        private static ICodeGeneratorFactory _defaultProvider;
        private static readonly object lockObject = new object();

        private CodeGeneratorFactory()
        {

        }

        public static ICodeGeneratorFactory CreateInstance()
        {
            if (_defaultProvider == null)
            {
                lock (lockObject)
                {
                    if (_defaultProvider == null)
                    {
                        _defaultProvider = new CodeGeneratorFactory();
                    }
                }
            }
            return _defaultProvider;
        }

        public ICodeBuilder CreateProjectBuilder(ApplicationConfiguration config)
        {
            var solutionBuilder = new SolutionBuilder(config);

            var commonConfig = new ClassLibraryConfiguration { Name = "Common", Version = ClassLibraryVersion.DotNetFourPointZero };
            var commonProjectBuilder = new CommonProjectBuilder(config, commonConfig);
            solutionBuilder.AddProject(commonProjectBuilder);
            solutionBuilder.Successor = commonProjectBuilder;

            var domainConfig = new ClassLibraryConfiguration { Name = "Domain", Version = ClassLibraryVersion.DotNetFourPointZero };
            var domainProjectBuilder = new DomainProjectBuilder(config, domainConfig);
            solutionBuilder.AddProject(domainProjectBuilder);
            commonProjectBuilder.Successor = domainProjectBuilder;

            //return solutionBuilder;

            var dataConfig = new ClassLibraryConfiguration { Name = "Data", Version = ClassLibraryVersion.DotNetFourPointZero };
            var dataProjectBuilder = new DataProjectBuilder(config, dataConfig);
            solutionBuilder.AddProject(dataProjectBuilder);
            domainProjectBuilder.Successor = dataProjectBuilder;

            //var dataTransferConfig = new ClassLibraryConfiguration { Name = "DataTransfer", Version = ClassLibraryVersion.DotNetFourPointZero };
            //var dataTransferProjectBuilder = new DomainProjectBuilder(config, dataTransferConfig);
            //solutionBuilder.AddProject(dataTransferProjectBuilder);
            //dataProjectBuilder.Successor = dataTransferProjectBuilder;

            var presentationConfig = new ClassLibraryConfiguration { Name = "Presentation", Version = ClassLibraryVersion.DotNetFourPointZero };
            var presentationProjectBuilder = new PresentationProjectBuilder(config, presentationConfig);
            solutionBuilder.AddProject(presentationProjectBuilder);
            //dataTransferProjectBuilder.Successor = presentationProjectBuilder;
            dataProjectBuilder.Successor = presentationProjectBuilder;

            var webConfig = new ClassLibraryConfiguration { Name = "Web", Version = ClassLibraryVersion.DotNetFourPointZero };
            var webProjectBuilder = new MVC5ProjectBuilder(config, webConfig);
            solutionBuilder.AddProject(webProjectBuilder);
            presentationProjectBuilder.Successor = webProjectBuilder;

            //ZipHelper.UnzipFile("Resources/MV5ProjectResources/MV5ProjectResources.zip", GetDirectoryPath());
            return solutionBuilder;
        }

        public IProjectBuilderRequest CreateRequest()
        {
            return new ProjectBuilderRequest();
        }
    }
}
